package com.vito.oa.act.action;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.Result;
import com.vito.base.util.Contants;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.model.ProcessGroupModel;
import com.vito.oa.act.service.OaActProcessGroupService;

@Controller
@RequestMapping("oa/act/processGroup")
public class OaActProcessGroupAction extends BaseAction {

	@Reference
	private OaActProcessGroupService oaActProcessGroupService;

	/**
	 * 查询组
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryGroupByDept")
	public Result queryGroupByDept(ProcessGroupModel group) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg("请重新登陆");
				return rs;
			}
			group.setCompanyId(user.getDeptParentId());
			List<ProcessGroupModel> list = oaActProcessGroupService.queryGroups(group);
			rs.setData(list);
			rs.setCode(Result.CODE_OK);
		} catch (Exception e) {
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("程序执行异常");
		}
		return rs;
	}

	/**
	 * 查询组与流程
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryGroupsAndProcess")
	public Result queryGroupsAndProcess(@RequestParam Map<String, String> param) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg("请重新登陆");
				return rs;
			}
			param.put("companyId", user.getDeptParentId());
			rs.setData(oaActProcessGroupService.queryGroupsAndProcess(param));
			rs.setCode(Result.CODE_OK);
		} catch (Exception e) {
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("程序执行异常");
		}
		return rs;
	}

	/**
	 * 新建组
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveDefinedGroup")
	public Result saveDefinedGroup(@RequestBody ProcessGroupModel group) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg("请重新登陆");
				return rs;
			}
			group.setCreateUser(user.getUserId());
			group.setCompanyId(user.getDeptParentId());
			group.setDefined(Contants.PROCESS_IS_DEFINED0);
			oaActProcessGroupService.saveDefinedGroup(group);
			rs.setMsg("保存成功");
			rs.setCode(Result.CODE_OK);
		} catch (Exception e) {
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("程序执行异常");
		}
		return rs;
	}

	/**
	 * 修改组
	 * 
	 * @param group
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateDefinedGroup")
	public Result updateDefinedGroup(@RequestBody ProcessGroupModel group) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg("请重新登陆");
				return rs;
			}
			group.setCompanyId(user.getDeptParentId());
			oaActProcessGroupService.updateGroupAndProcess(group);
			rs.setMsg("修改成功");
			rs.setCode(Result.CODE_OK);
		} catch (Exception e) {
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("程序执行异常");
		}
		return rs;
	}

	/**
	 * 修改组排序
	 * 
	 * @param [groupId]
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateGroupOrder")
	public Result updateGroupOrder(String[] groupId) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg("请重新登陆");
				return rs;
			}
			ProcessGroupModel group = new ProcessGroupModel();
			if (groupId != null && groupId.length > 0) {
				for (int i = 0; i <= groupId.length-1; i++) {
					group.setGroupId(groupId[i]);
					group.setOrderNum(i);
					oaActProcessGroupService.updateGroupBase(group);
				}
			}
			rs.setMsg("保存成功");
			rs.setCode(Result.CODE_OK);
		} catch (Exception e) {
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("程序执行异常");
		}
		return rs;
	}

	/**
	 * 删除用户自定义组
	 * 
	 * @param groupId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteDefinedGroup")
	public Result deleteDefinedGroup(String groupId) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg("请重新登陆");
				return rs;
			}
			ProcessGroupModel group = oaActProcessGroupService.queryGroupById(groupId);
			if (group != null) {
				if (OaActContants.PROCESS_GROUP_SYS.equals(group.getDefined())) {
					rs.setCode(Result.CODE_PARA_ERROR);
					rs.setMsg("默认组无法删除");
					return rs;
				}
			}
			if (oaActProcessGroupService.deleteDefinedGroup(groupId) > 0) {
				rs.setMsg("删除成功");

			} else {
				rs.setMsg("该组使用中无法删除");
			}
			rs.setCode(Result.CODE_OK);
		} catch (Exception e) {
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("程序执行异常");
		}
		return rs;
	}

	/**
	 * 移动流程到组
	 * 
	 * @param {groupId,checkeds[key,...]}
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addProcessToGroup")
	public Result addProcessToGroup(@RequestBody ProcessGroupModel process) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg("请重新登陆");
				return rs;
			}
			ProcessGroupModel group = oaActProcessGroupService.queryGroupById(process.getGroupId());
			if (group != null) {
				process.setCompanyId(user.getDeptParentId());
				oaActProcessGroupService.addProcessToGroup(process);
				rs.setMsg("保存成功");
			} else {
				rs.setMsg("该组不存在");
			}
			rs.setCode(Result.CODE_OK);
		} catch (Exception e) {
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("程序执行异常");
		}
		return rs;
	}
}
